package initialize

import (
	"context"
	"gin-template/config"
	"gin-template/global"
	"log"
	"time"

	"github.com/go-redis/redis/v8"
)

func InitRedis() {
	redisConfig := config.AppConfig.Redis

	RedisClient := redis.NewClient(&redis.Options{
		Addr:     redisConfig.Host + ":" + redisConfig.Port,
		Password: redisConfig.Password,
		DB:       redisConfig.DB,
	})

	for {
		_, err := RedisClient.Ping(context.Background()).Result()
		if err != nil {
			log.Printf("Redis连接失败: %v", err)
			time.Sleep(time.Second * 5)
		} else {
			global.RedisDB = RedisClient
			break
		}
	}

	log.Println("Redis连接成功")
}
